

var gCurrentUser = null;
var gUsers;

function FavouritesPageBuilder () {
	PageBuilder.call(this);
}

FavouritesPageBuilder.prototype = new PageBuilder();

FavouritesPageBuilder.prototype.constructor = FavouritesPageBuilder;

FavouritesPageBuilder.prototype.build = function() {
	// doesn't do anything yet
};


$(document).ready(function() {
	setLogin();
	buildPage(new FavouritesPageBuilder());
	$("#userFavouritesDialog").dialog({
		autoOpen: false,
		modal: true,
		width: 600,
		height: 500
	});
	var name_label = getDisplayString("label_name");
	var since_label = getDisplayString("label_since");
	var view_label = getDisplayString("label_view");
	var others_favourites_caption = getDisplayString("caption_others_favourites");

	$("#userList").jqGrid({
		datatype: "local",
		height: 300,
		autowidth: true,
		// icon title date view publish edit delete
		colName:['Name','Registered Since', 'View Favourites'],
		colModel:[
			{name:name_label, index:'user_name'},
			{name:since_label, index:'reg_since'},
			{name:view_label, index:'view_widgets'}
		],
		multiselect: false,
		caption: others_favourites_caption,
		pager:'#pager'
	});
	$("#userList").navGrid('#pager',{edit:false,add:false,del:false,search:false});

	
	// grid resizer
	$(window).resize(function(e) {
        var newPageWidth = $("#page").width();
		$("#userList").jqGrid('setGridWidth', newPageWidth);

    });	
	
	getAndBuildUserList();
});



function getAndBuildUserList() {
	var restURL = store_rest_url+"/users";
	$.ajax({
		url: restURL,
		type:"GET",
		dataType:"json",
		success: listUsers
	});
}


function listUsers (users )
{
	gUsers = users;
	if ($.isArray(users) ) {
		//userRows = '';
		$("#userList").jqGrid('clearGridData');
		for ( var i = 0; i < users.length; i++ ) {
			var user = users[i];
			var date = new Date(user.accountInfo.joined);
			
			var view_button = '<button onClick="showUserFavourites(\''+user.username+'\')">Favourites</button>';
			var userRowData = {
				Name: user.accountInfo.realname,
				Since: date.toDateString(),
				View: view_button
			};
			$("#userList").jqGrid('addRowData', i+1,userRowData); 

			//userRows += '<tr>';
			//userRows += '<td>'+user.accountInfo.realname+'</td>';
			//var date = new Date(user.accountInfo.joined);
			//userRows += '<td>'+date.toUTCString()+'</td>';
			//userRows += '<td><button onClick="showUserFavourites('+user.accountInfo.id+')">View</button></td>';
			//userRows += '</tr>';
		}
		//$("#uvnsr").html(userRows);
	}
	//$("#userList").dataTable();
}

function showUserFavourites(userid) {
	gCurrentUser = null;
	if ( gUsers != null ) {
		for ( var i = 0; i < gUsers.length; i++ ) {
			if ( gUsers[i].username == userid ) {
				gCurrentUser = gUsers[i];
			}
		}
	}
	var restURL = store_rest_url+"/users/favourites/"+userid;
	$.ajax({
		url: restURL,
		type:"GET",
		beforeSend: function(xhr) {
			var authString = getBase64Authentication();
			xhr.setRequestHeader("Authorization", "Basic "+authString);
		},
		dataType:"json",
		success: showUserFavouritesDialog
	});
}


function showUserFavouritesDialog (favourites)
{
	$("#widgetList").html('');
	$("#widgetView").html('');
	if ( gCurrentUser != null ) {
		$("#userFavouritesDialog").dialog("option","title", gCurrentUser.accountInfo.realname+'\'s Favourites' );
	}

	if ( $.isArray(favourites) && favourites.length > 0 ) {
		for ( var i = 0; i < favourites.length; i++ ) {
			var widget = favourites[i];
			var html = '<div class="widget_link" onclick="viewAWidget(\''+widget.id+'\')">';
			html += '<img src="'+widget.icon+'" width="22" height="22"/ onerror="this.src=\"img/default_widget_icon.png\"">';
			html += '&nbsp;<span class="widget_title">'+widget.name+'</span>';
			html += '</div>';
			$("#widgetList").append(html);
		}
	}
	else {
		var html = '<div class="widget_link">...</div>';
		$("#widgetList").append(html);
	}
	$("#userFavouritesDialog").dialog("open");
}



function viewAWidget(widgetId) {
	$.ajax({
		type:"GET",
		url:store_rest_url+"/widgets/"+widgetId,
		dataType: "json",
		success: putWidgetInView
	});
}

function putWidgetInView ( widget ) {
	var widget_id = widget.widgetProfile.id;
	var button_label = getDisplayString("label_favourites");
	$("#widgetView").html(widget.renderInfo+'<div><button onClick="setWidgetFavourite('+widget_id+')">+ '+button_label+'</button></div>');
}
